<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button v-has="30501" type="primary" size="small" @click="add">新增</el-button>
        </div>
        <el-table :data="table_body" stripe border :max-height="tableHeight">
          <el-table-column prop="name" label="科目" />
          <el-table-column prop="createdAt" label="创建时间" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="30502" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :visible.sync="openDrawer" :title="drawerTitle" width="60%">
      <el-form>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="科目" label-position="top">
              <el-input v-model="formData.name" placeholder="科目" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-right: 8px" @click="openDrawer = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      openDrawer: false,
      table_header: [
        {
          title: '科目',
          key: 'name'
        },
        {
          title: '创建时间',
          key: 'createdAt'
        },
        {
          title: '操作',
          slot: 'action'
        }
      ],
      table_body: [

      ],
      formDataDefault: {
        id: '',
        name: ''
      },
      formData: {
        id: '',
        name: ''
      },
      queryData: {
        pageSize: 50,
        currentPage: 0,
        enable: 'true'
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      drawerTitle: '新增'
    }
  },
  created() {
    this.list()
    this.$store.dispatch('getCustomerOptions')
  },
  mounted() {
  },
  methods: {
    list() {
      request({
        url: 'charge-subject/query?pageNum=' + this.queryData.currentPage + '&pageSize=' + this.queryData.pageSize,
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.table_body = res.data.records
            this.pageTotal = res.data.total
          }
        }
      })
    },
    submit() {
      request({
        url: 'charge-subject/add',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openDrawer = false
          this.list()
        }
      })
    },
    edit(row) {
      this.drawerTitle = '编辑'
      this.copyProperty(this.formData, row)
      this.openDrawer = true
    },
    copyProperty(to, from) {
      for (const x in from) {
        if (!to.hasOwnProperty(x)) {
          continue
        }
        if (from[x] instanceof Array) {
          to[x] = from[x]
        } else if (from[x] instanceof Object) {
          this.copyProperty(to[x], from[x])
        } else if (from[x] == null) {
          continue
        } else if (typeof (from[x]) === 'boolean') {
          to[x] = from[x] + ''
        } else {
          to[x] = from[x]
        }
      }
    },
    add() {
      for (const x in this.formData) {
        this.formData[x] = this.formDataDefault[x]
      }
      this.openDrawer = true
    },
    openDefDrawer() {
      this.openChargeDefDrawer = true
    },
    closeDefDrawer() {
      this.openChargeDefDrawer = false
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 230
    },
    pageSize() {
      return this.pageSizeOpts[0]
    },
    ...mapState(//
      {
        customerOptions: state => state.base.customerOptions
      }
    )
  }
}
</script>
<style>
    .demo-drawer-footer{
        width: 100%;
        position: absolute;
        bottom: 0;
        left: 0;
        border-top: 1px solid #e8e8e8;
        padding: 10px 16px;
        text-align: right;
        background: #fff;
    }

    .demo-upload-list{
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0,0,0,.2);
        margin-right: 4px;
    }
    .demo-upload-list img{
        width: 100%;
        height: 100%;
    }
    .demo-upload-list-cover{
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0,0,0,.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover{
        display: block;
    }
    .demo-upload-list-cover i{
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>
